package com.jsmile.cloud.msgcenter.msg.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.jsmile.cloud.authc.api.enums.SmsTypeEnum;
import com.jsmile.cloud.msgcenter.api.constants.MsgConstants;
import com.jsmile.cloud.msgcenter.api.enums.MsgCodeEnum;
import com.jsmile.cloud.msgcenter.api.to.SmsTo;
import com.jsmile.cloud.msgcenter.web.config.MsgConfig;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.JSmileCacheEnum;
import com.jsmile.mall.api.enums.JSmileCodeEnum;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.cache.kit.CacheKit;
import com.jsmile.mall.log.annotation.JsmileLog;

import cn.hutool.captcha.generator.RandomGenerator;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
public class MsgServiceApiImpl extends AuthcController {

    private final AmqpTemplate amqpTemplate;
    private final MsgConfig msgConfig;

    @PostMapping("/feign/msg/sendSms")
    @JsmileLog(title = "消息中心->短信发送", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.MSG_CENTER)
    public JSmileResult sendSms(@RequestBody @Valid SmsTo smsTo) {
        // 注册短信缓存1分钟
        Integer count = CacheKit.getInt(JSmileCacheEnum.fiveMinute, SmsTypeEnum.getCacheName(smsTo.getSmsType()) + smsTo.getPhone() + "_tenantId_" + smsTo.getTenantId());
        count = null == count ? 1 : ++count;
        if (count > msgConfig.getSmsSendMaxCount()) {
            return JSmileResult.error(MsgCodeEnum.SMS_SEND_MORE.getResCode(), MsgCodeEnum.SMS_SEND_MORE.getResMsg());
        }
        Map<String, String> smsParams = Maps.newHashMap();
        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
        smsParams.put("code", randomGenerator.generate());
        smsTo.setSmsParams(smsParams);
        // 注册短信缓存1分钟
        CacheKit.put(JSmileCacheEnum.fiveMinute, SmsTypeEnum.getCacheName(smsTo.getSmsType()) + smsTo.getPhone() + "_tenantId_" + smsTo.getTenantId(), count);
        amqpTemplate.convertAndSend(MsgConstants.SmsMq.SMS_SEND_EXCHANGE, MsgConstants.SmsMq.SMS_SEND_ROUTE, smsTo);
        return JSmileResult.success();
    }

    @PostMapping("/feign/msg/verifyCode")
    @JsmileLog(title = "消息中心->短信验证码验证", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.MSG_CENTER)
    public JSmileResult verifyCode(@RequestBody @Valid SmsTo smsTo) {
        Object code = CacheKit.get(JSmileCacheEnum.fiveMinute, SmsTypeEnum.getCacheName(smsTo.getSmsType()) + smsTo.getPhone() + "_tenantId_" + smsTo.getTenantId());
        if (null == code) {
            return JSmileResult.error(JSmileCodeEnum.KAPTCHA_EXPIRE);
        }
        if (!smsTo.getSmsParams().get("code").equals(code.toString())) {
            return JSmileResult.error(JSmileCodeEnum.KAPTCHA_ERROR);
        }
        CacheKit.remove(JSmileCacheEnum.fiveMinute, SmsTypeEnum.getCacheName(smsTo.getSmsType()) + smsTo.getPhone() + "_tenantId_" + smsTo.getTenantId());
        return JSmileResult.success();
    }

}
